#include "UserMysql.h"
#include "ui_UserMysql.h"
#include "Login-ly.h"
#include<QDebug>
#include<QSqlError>
#include<QMessageBox>
#include <QFile>
#include <QDir>
#include<QDebug>

UserMysql::UserMysql(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserMysql)
{
    ui->setupUi(this);

    setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
    setAttribute(Qt::WA_TranslucentBackground);

    setWindowIcon(QIcon(":/UIsources/logo2.png"));

    QString Path = QDir::homePath() + "/Desktop/data.txt";
        QFile file(Path);
        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream in(&file);
            QString line = in.readLine();
            QStringList data = line.split("\t");
            if (data.size() >= 2)
            {
                ui->edit_hostname->setText(data[0]);
                ui->edit_user->setText(data[1]);
                ui->edit_password->setText(data[2]);
                ui->edit_database->setText(data[3]);
                ui->edit_port->setText(data[4]);
            }
            file.close();

        }


    connect(ui->btn_host_login,&QPushButton::clicked,[=]()
    {
        QString host=ui->edit_hostname->text();
        QString user=ui->edit_user->text();
        QString password=ui->edit_password->text();
        QString database=ui->edit_database->text();
        int port=3306;
        if((ui->edit_port->text().toInt())!=3306)
        {
            port=ui->edit_port->text().toInt();
        }

        if(host.isEmpty()||user.isEmpty()||password.isEmpty()||database.isEmpty())
        {
            QMessageBox::warning(this,"warning","必填项未填!");
        }
        else
        {

            QString desktopPath = QDir::homePath() + "/Desktop";
              QFile file(desktopPath + "/data.txt");

            if (file.open(QIODevice::WriteOnly | QIODevice::Text))
            {
                QTextStream out(&file);
                // 创建一个 QTextStream 对象，关联到打开的文件

                out <<host << "\t" <<user << "\t" << password<<"\t"<<database<<"\t"<<port<<"\n";
                // 将三个 QLineEdit 的数据写入文件，用制表符分隔，最后换行

                file.close();
                // 关闭文件
            }
            QString desktopPath2 = QDir::homePath() + "/Desktop/data.txt";
            QFile file2(desktopPath);
            if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                QTextStream in(&file2);
                QString line = in.readLine();
                QStringList data = line.split("\t");
                if (data.size() >= 2)
                {
                   host=data[0];
                   user=data[1];
                   password=data[2];
                   database=data[3];
                   port=data[4].toInt();
                }
                file.close();
            }

            db=QSqlDatabase::addDatabase("QMYSQL");
            db.setHostName(host);
            db.setUserName(user);
            db.setPassword(password);
            db.setDatabaseName(database);
            db.setPort(port);
            if(!db.open())
            {
                qDebug()<<db.lastError()<<endl;
                return;
            }
            else
            {
              qDebug()<<"打开成功"<<endl;
              this->SetFakeStuInfo();
            }
        }

    });
   // connect(ui->btn_host_return,&QPushButton::clicked,this,&mysql::Returnface);
}

UserMysql::~UserMysql()
{
    delete ui;
}

//void mysql::Returnface()
//{
//    firstwidget* w = new firstwidget;
//    w->show();
//    this->hide();
//}

void UserMysql::on_btn_host_return_clicked()
{
    Login_ly *log = new Login_ly;
    //log->Setdb(this->db);
    this->close();
    log->show();

}

void UserMysql::SetFakeStuInfo()
{
    QSqlQuery query(db);
    //删除现有表
    query.exec("drop table log;");
    query.exec("drop table student_list;");
    query.exec("drop table StuInfo;");
    query.exec("drop table Lesson;");
    query.exec("drop table Score;");

    //创建新表
    query.exec("create table log(id int primary key,username text, passwd text,image longblob,name text,StuId int,TchId int,ident text);");
    query.exec("create table student_list(class int,id int primary key,Name text,Sex text,teacher text,teacherId text);");
    query.exec("create table StuInfo(id int primary key,name text, sex text,birthday date,city text,residence text,phone text,email text, stageId text,image longblob);");
    query.exec("create table Lesson(id int primary key,name text);");
    query.exec("create table Score(id int primary key,StuId text,StageId text,Score int, comment text);");
    //登录表
    query.exec("insert into log(id,username,passwd,name,TchId,ident) values(1,'123456789@qq.com','123456abc@','吕冠钟',02680100,'老师')");
    query.exec("insert into log(id,username,passwd,name,StuId,ident) values(2,'123456@163.com','abc123456#','张三',02680001,'学生')");
    query.exec("insert into log(id,username,passwd,name,StuId,ident) values(3,'5201314@qq.com','666666@abc','李四',02680002,'学生')");
    //老师表
    query.exec("insert into student_list(class,id,Name,Sex,teacher,teacherId) values(268,1,'吕冠钟','男','吕冠钟','123456789@qq.com')");
    query.exec("insert into student_list(class,id,Name,Sex,teacher) values(268,2,'张三','男','吕冠钟')");
    query.exec("insert into student_list(class,id,Name,Sex,teacher) values(268,3,'李四','男','吕冠钟')");
    //学生信息表
    query.exec("insert into StuInfo(id,name,sex,birthday,city,residence,phone,email,stageId) values(1,'张三','男','2002-04-28','江苏南京','安徽合肥','15665458718','123456@163.com','1001')");
    query.exec("insert into StuInfo(id,name,sex,birthday,city,residence,phone,email,stageId) values(2,'李四','男','2002-08-30','江苏南京','上海','18856904789','5201314@qq.com','1002')");
    //学习阶段表
    query.exec("insert into Lesson(id,name) values(1001,'第一阶段');");
    query.exec("insert into Lesson(id,name) values(1002,'第二阶段');");
    query.exec("insert into Lesson(id,name) values(1003,'第三阶段');");
    //成绩评价表
    query.exec("insert into Score(id,StuId,StageId) values(1,'123456@163.com','1001')");
    query.exec("insert into Score(id,StuId,StageId,Score,comment) values(2,'5201314@qq.com','1001',90,'整挺好')");
    query.exec("insert into Score(id,StuId,StageId,Score,comment) values(3,'5201314@qq.com','1002',95,'发挥稳定')");

       qDebug()<<"数据库打开成功"<<endl;
 }




